#include "list/list.h"

#include <iostream>

ListNode* createList(const int arr[], const int n)
{
    if (n <= 0)
        return nullptr;

    ListNode* head = new ListNode(arr[0]);
    ListNode* pre = head;
    ListNode* curr = head->next;

    for (int i = 1; i < n; i++)
    {
        curr = new ListNode(arr[i]);
        pre->next = curr;
        // 向后移动
        pre = curr;
    }

    return head;
}

void deleteList(ListNode*& head)
{
    if (nullptr == head)
        return;

    ListNode *curr, *next;
    curr = head;
    while (curr != nullptr)
    {
        next = curr->next;
        delete curr;
        curr = next;
    }

    head = nullptr;
}

void printList(const ListNode* head)
{
    if (nullptr == head)
        return;

    const ListNode* curr = head;
    while (curr != nullptr)
    {
        std::cout << curr->data << " ";
        curr = curr->next;
    }

    std::cout << std::endl;
}